MediumGiven a function fn, return a memoized version of that function.
A memoized function is a function that will never be called twice with the same inputs. Instead it will return a cached value.
You can assume there are 3 possible input functions: sum, fib, and factorial.
sum accepts two integers a and b and returns a + b.fib accepts a single integer n and returns 1 if n <= 1 or fib(n - 1) + fib(n - 2) otherwise.factorial accepts a single integer n and returns 1 if n <= 1 or factorial(n - 1) * n otherwise.給定一個函數“fn”作為參數,返回該函數的記憶版本。
記憶函數是永遠不會使用相同輸入呼叫兩次的函數。 相反,它將返回一個快取的值。
可以假設有 3 個可能的輸入函數:sum, fib, 和 factorial。
sum 接受兩個整數參數 a 和 b 並回傳 a + b。fib 接受單一整數n作為參數,如果n <= 1則傳回1,否則傳回fib(n - 1) + fib(n - 2)。factorial 接受單一整數 n,如果 n <= 1 則傳回 1,否則傳回 factorial(n - 1) * n。創建一個函式memoize,他接收一個函示fn作為參數,
返回一個記憶函示memoizedFn,memoizedFn有一個附加的.getCallCount()方法提供外部查詢調用次數。
function memoize(fn) {
const memoizedFn = function () {
//Memoization邏輯
};
memoizedFn.getCallCount = function () {
//查詢調用次數的邏輯
};
return memoizedFn;
}
緩存存取邏輯
cache用來存取每一個調用時傳入的參數。fn的參數...arg,key的內容為唯一值,fn(...args)結果後存入cache[key],再返回cache[key]。function memoize(fn) {
const cache = {};
const memoizedFn = function (...args) {
try{
const key = JSON.stringify(args);
if (key in cache) {
return cache[key];
} else {
cache[key] = fn(...args);
return cache[key];
}
}catch(error){
return {
error: 'Memoization error',
message: error.message,
args: args,
};
}
};
memoizedFn.getCallCount = function () {
//查詢調用次數的邏輯
};
return memoizedFn;
}
查詢調用次數
callCount,用於紀錄調用次數。function memoize(fn) {
const cache = {};
let callCount = 0;
const memoizedFn = function (...args) {
try{
const key = JSON.stringify(args);
if (key in cache) {
return cache[key];
} else {
callCount++; //增加調用次數
cache[key] = fn(...args);
return cache[key];
}
}catch(error){
return {
error: 'Memoization error',
message: error.message,
args: args,
};
}
};
memoizedFn.getCallCount = function () {
return callCount;
};
return memoizedFn;
}
某些情況下,使用 Map 可能會更具性能優勢,特別是當緩存的鍵是複雜的對像或非字符串類型。
創建一個函式memoize,他接收一個函示fn作為參數,
返回一個記憶函示memoizedFn,memoizedFn有一個附加的.getCallCount()方法提供外部查詢調用次數。
function memoize(fn) {
const memoizedFn = function () {
//Memoization邏輯
};
memoizedFn.getCallCount = function () {
//查詢調用次數的邏輯
};
return memoizedFn;
}
緩存存取邏輯
cache為一個Map實例,存儲對應的key和value,進行緩存邏輯判斷後返回value。
const args = [2, "hello", { key: "value" }];,fn(...args)結果後存入cachecache.set(key, result),function memoize(fn) {
const cache = new Map();
const memoizedFn = function (...args) {
try{
const key = args.map(a => typeof a + '|' + JSON.stringify(a)).join(',');
if (cache.has(key)) {
return cache.get(key);
} else {
const result = fn(...args);
cache.set(key, result);
return result;
}
}
catch(error){
return {
error: 'Memoization error',
message: error.message,
args: args,
};
}
memoizedFn.getCallCount = function () {
//查詢調用次數的邏輯
};
return memoizedFn;
}
}
查詢調用次數
callCount,用於紀錄調用次數。function memoize(fn) {
const cache = new Map();
let callCount = 0;
const memoizedFn = function (...args) {
try{
const key = args.map(a => typeof a + '|' + JSON.stringify(a)).join(',');
if (cache.has(key)) {
return cache.get(key);
} else {
callCount+=1;
const result = fn(...args);
cache.set(key, result);
return result;
}
}
catch(error){
return {
error: 'Memoization error',
message: error.message,
args: args,
};
}
};
memoizedFn.getCallCount = function () {
return callCount;
};
return memoizedFn;
}